package com.zilue.payment.gateway.paramter;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.zilue.payment.gateway.annotation.ParameterFiledCheck;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zilue on 2017/3/6.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class PublicParameter extends BaseParameter {
    @ParameterFiledCheck(regex = "\\w{1,32}", description = "1-32位的数字或字母的组合")
    private String appid;
    @ParameterFiledCheck(regex = "[a-zA-Z.]{1,32}", description = "1-32位以内的字母和.的组合")
    private String method;
    @ParameterFiledCheck(regex = "(?i)JSON", description = "目前仅支持JSON")
    private String format;
    @ParameterFiledCheck(regex = "((?i)utf-8){0,1}", notNull = false, description = "目前仅支持UTF-8")
    private String charset;
    @ParameterFiledCheck(regex = "(?i)RSA", description = "目前仅支持RSA")
    private String sign_type;
    private String sign;
    @ParameterFiledCheck(regex = "^(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}:([0-5]\\d{1})$", description = "yyyy-MM-dd HH:mm:ss(注意空格)")
    private String timestamp;
    @ParameterFiledCheck(regex = "1.0", description = "固定为1.0")
    private String version;
    private JSONObject biz_content;
    private String bizContent;

    public Map<String, String> getParamterMap() {
        Map<String, String> map = new HashMap<String, String>();
            Class<? extends PublicParameter> clazz = this.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            PublicParameter publicParameter=this;
            Arrays.stream(declaredFields).forEach(field -> {
                try {
                    field.setAccessible(true);
                    String name = field.getName();
                    Object  valueObjct = field.get(publicParameter);
                    String value=(valueObjct==null?"":String.valueOf(valueObjct));
                    if (!"biz_content".equals(name)) {
                        if ("bizContent".equals(name)) {
                            map.put("biz_content", String.valueOf(value));
                        }else{
                            if (value != null)
                            map.put(name, String.valueOf(value));
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            });
        return map;
    }


    public static void main(String[] args) {
        PublicParameter publicParameter = new PublicParameter();
        publicParameter.setAppid("sd");
        publicParameter.setMethod("sd.pay");
        publicParameter.setFormat("Json");
        publicParameter.setCharset("Utf-8");
        publicParameter.setSign_type("RSA");
        publicParameter.setTimestamp("2017-03-31  21:22:10");
        ParameterCheckResult result = publicParameter.ParameterCheck();
        System.out.println(result);
    }

}
